//
//  CCViewStudyViewController.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 3/29/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCViewStudyViewController.h"
#import "CCVSCell.h"
#import "CCVSHeader.h"
#import <QuartzCore/QuartzCore.h>

#import "CCVSCommon.h"
#import "CCVSCustomLayout.h"
#import "CCVSCustomView.h"
#import "CCVSCustomView.h"

#import "ClinTrialWebViewController.h"
///imports for helpers to load data
#import "CCVSListOfStrings.h"
#import "CCVSHyperLink+Utilities.h"
#import "CCVSGraphData+Utilities.h"
#import "CCVSImage.h"
#import "CCVSString.h"


@interface CCViewStudyViewController () <UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic) BOOL updateInProgress;
@property (strong,nonatomic) NSArray * studySections;
@property (nonatomic,readonly) enum CCVS_LAYOUT_SCALE scaleType;
@property (weak, nonatomic) IBOutlet UINavigationItem *titleBar;

@end

@implementation CCViewStudyViewController

@synthesize studySections = _studySections;
@synthesize ccvsColumns = _ccvsColumns;
@synthesize disableScaleToFit = _disableScaleToFit;
@synthesize scaleType = _scaleType;
@synthesize filterEmptySections = _filterEmptySections;
@synthesize titleBar = _titleBar;

-(enum CCVS_LAYOUT_SCALE)scaleType
{
    return (self.disableScaleToFit) ? CCVS_LAYOUT_FULL_SCALE : CCVS_LAYOUT_SCALE_TO_SUPERVIEW;
}

-(BOOL)filterEmptySections
{
    //by default...  if there is only 1 column (study) being displayed
    //then we should skip over any empty sections
    //however, if there is more than one column/study
    //then we will want to display all sections
    if (!_ccvsColumns) return YES;
    return ([self.ccvsColumns count] < 2);
}

-(void)setDisableScaleToFit:(BOOL)disableScaleToFit
{
    //controls the layout settings
    _disableScaleToFit = disableScaleToFit;
    if ([self.collectionView.collectionViewLayout isKindOfClass:[CCVSCustomLayout class]]) {
        ((CCVSCustomLayout*)self.collectionView.collectionViewLayout).scaleType = self.scaleType;
    }
}

-(void)setCcvsColumns:(NSArray *)ccvsColumns
{
    _ccvsColumns = ccvsColumns;
    self.studySections = [(CCVSColumn*)ccvsColumns[0] getSectionsFilterEmptySections:self.filterEmptySections];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //return 1;  //this will be handled in the subview of the cell itself
    return [self.ccvsColumns count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.studySections count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    //
    CCVSCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    [cell resetCell];
    
    //TODO: fix below for CCVSColumn and do same fix inside of CustomView.m
    cell.vc = self;
    CCVSColumn * col = (CCVSColumn*)self.ccvsColumns[indexPath.row];
    cell.topics = [col topicDataInSection:indexPath.section filterEmptySections:self.filterEmptySections];
    cell.details = [col detailDataInSection:indexPath.section filterEmptySections:self.filterEmptySections];
    
    cell.backgroundColor = [UIColor lightGrayColor];
    
    return cell;
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        CCVSHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CCVSSectionHeader" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headerView.isCVTitle = YES;
        } else {
            headerView.isCVTitle = NO;
        }
        headerView.headerString = self.studySections[indexPath.section];
        reusableview = headerView;
    }
    //    if (kind == UICollectionElementKindSectionFooter) {
    return reusableview;
}

//this seems to be required b/c recycled views are not always cleaned fast enough
//which leaves behind stale contents i.e., stray subviews
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [(CCVSCell*)cell resetCell];
}

#pragma mark – UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size;
    CCVSColumn * col = (CCVSColumn*)self.ccvsColumns[0];
    size.width = col.width;
    if (size.width < 1) {
        //if the size is not specified, then we want it to be full size
        size.width = [CCVSCustomLayout getWidthOfColumn:indexPath.item
                                               withData:self.ccvsColumns inSuperView:self.view fitToSuperView:self.scaleType];
    }
    size.height = [CCVSCell calcHeightOfCell:[col topicDataInSection:indexPath.section filterEmptySections:self.filterEmptySections]
                                 withDetails:[col detailDataInSection:indexPath.section filterEmptySections:self.filterEmptySections]
                               withCellWidth:size.width];
    
    CGSize size2;
    for (int item = 0; item < [self.ccvsColumns count]; item++) {
        CCVSColumn * col2 = (CCVSColumn*)self.ccvsColumns[item];
        size2.width = col2.width;
        if (size2.width < 1) {
            //if the size is not specified, then we want it to be full size
            size2.width = [CCVSCustomLayout getWidthOfColumn:item
                                                    withData:self.ccvsColumns inSuperView:self.view fitToSuperView:self.scaleType];
        }
        size2.height = [CCVSCell calcHeightOfCell:[col2 topicDataInSection:indexPath.section filterEmptySections:self.filterEmptySections]
                                      withDetails:[col2 detailDataInSection:indexPath.section filterEmptySections:self.filterEmptySections]
                                    withCellWidth:size2.width];
        
        size.height = MAX(size.height,size2.height);
    }
    return size;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size;
    size.width = self.view.frame.size.width;
    //section 0 is the title
    if (section == 0) {
        size.height = 50;
    } else if ([@"-" isEqualToString:(NSString*)self.studySections[section]]) {
        //we really have just a divider as opposed to a labeled section
        size.height = 10;
    } else {
        //other sections are smaller
        size.height = 30;
    }
    return size;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.collectionView.delegate = self;
    CCVSCustomLayout * layout =  [[CCVSCustomLayout alloc] init];
    layout.vc = self;
    layout.superView = self.view;
    layout.scaleType = self.scaleType;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView.collectionViewLayout = layout;
    
    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CCVS Custom View Delegate
- (void)handleTapGestureForURLViewType:(UITapGestureRecognizer *) gesture
{
    if (![gesture.view conformsToProtocol:@protocol(CCVSCustomURLViewType)]) {
        //don't crash but don't continue
        return;
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        //TODO: Use Messaging to notify the CCViewStudyViewController
        //to Segue to the webview
        id<CCVSCustomURLViewType> data =((id<CCVSCustomURLViewType>)gesture.view);
        
        NSLog(@"URL View Type: Need to Segue here to: %@",data.destination);
        [self performSegueWithIdentifier:@WEBVIEW_SEGUE sender:data];
    }
}
- (void)handleTapGestureForImageViewType:(UITapGestureRecognizer *) gesture
{
    if (![gesture.view conformsToProtocol:@protocol(CCVSCustomImageViewType)]) {
        //don't crash but don't continue
        return;
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        //TODO: Use Messaging to notify the CCViewStudyViewController
        //to Segue to the webview
        id<CCVSCustomImageViewType> iv = (id<CCVSCustomImageViewType>)gesture.view;
        NSLog(@"Image View Type: Need to Segue here to: %@ (%@)",iv.largeImageURL, iv.iview);
        //[self performSegueWithIdentifier:@"ViewLargeImage" sender:iv];
    }
}

- (void)segueToVCWith:(id<CCVSCustomDataType>)data segueName:(NSString*)destSegue
{
    [self performSegueWithIdentifier:destSegue sender:data];
}

//this will toggle the isExpanded variable in the custom data type object
//and then refresh the view to support resize events
- (void)toggleExpantionAndRefreshView:(id)sender forDataType:(id<CCVSCustomDataType>)data
{
    //first we toggle
    data.isExpanded = (data.isExpanded) ? NO : YES;
    //next we refresh
    [self refreshCollectionView:sender];
}

-(void)refreshCollectionView:(id)sender
{
    [self.collectionView performBatchUpdates:^{
        self.updateInProgress = YES;
        [self.collectionView reloadData];
    } completion:^(BOOL finished) {
        self.updateInProgress = NO;
    }];
}

#pragma mark - Segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Segue to the ClinicalCollections Web site Lilly COI
    //
    if ([segue.destinationViewController isKindOfClass:[ClinTrialWebViewController class]] &&
        [sender conformsToProtocol:@protocol(CCVSCustomURLViewType)]) {
        
        id<CCVSCustomURLViewType> data = (id<CCVSCustomURLViewType>)sender;
        ((ClinTrialWebViewController *)[segue destinationViewController]).url = [data.destination absoluteString];
        
        //        [[CommonAPI class] addRecents:clinicalTrialGov.nctID
        //                               search:clinicalTrialGov.search.searchString];
        
    } else if ([sender conformsToProtocol:@protocol(CCVSCustomDataType)]) {
        if ([[segue destinationViewController] respondsToSelector:@selector(setCcvsCustomData:)]) {
            [[segue destinationViewController] performSelector:@selector(setCcvsCustomData:) withObject:sender];
        }
    }

}


//This refreshPressed method is used for testing
- (IBAction)refreshPressed:(UIBarButtonItem *)sender {
    [CCVSCommon displayCCVStestData:self];
    //[self refreshCollectionView:sender];
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self refreshCollectionView:self];
}

#pragma mark - helper to add columns

-(void)addColumn:(CCVSColumn*)column
{
    if ([self.ccvsColumns count] == 0) {
        self.ccvsColumns = @[column];
        return;
    }
    NSMutableArray * ccvsColumns = [self.ccvsColumns mutableCopy];
    
    CCVSColumn * existingColumn = self.ccvsColumns[0];
    NSArray * existingSections = [existingColumn getSections];
    NSMutableArray * newSections = [[column getSections] mutableCopy];
    CCVSColumn * newColumn = [[CCVSColumn alloc] init];
    int j = 0;
    int i = 0;
    while (i < [existingSections count] && j < [newSections count]) {
        if ([existingSections[i] isEqualToString:newSections[j]]) {
            //the two are equal so add next entry and continue
            [newColumn addSection:existingSections[i] withTopics:[column topicDataInSection:j] withDetails:[column detailDataInSection:j]];
            [newSections removeObjectAtIndex:j];
            [column removeSetion:j];
            i++;
            //j++;  newsections count is decremented
        } else if ([existingSections[i] isEqualToString:@"-"]) {
            [newColumn addSection:@"-" withTopics:nil withDetails:nil];
            i++;  // but not j
        } else if ([newSections[j] isEqualToString:@"-"]) {
            [newColumn addSection:newSections[j] withTopics:[column topicDataInSection:j] withDetails:[column detailDataInSection:j]];
            //one of them is a dash
            //for all existing sections we need to add an empty section...
            for (CCVSColumn *c in self.ccvsColumns) {
                [c addSectionAtIndex:i withTitle:@"-" withTopics:nil withDetails:nil];
            }
            [newSections removeObjectAtIndex:j];
            [column removeSetion:j];
            i++; //we added a section so we need to advance or we will now be pointing at what we added
            //j++;  newsections count is decremented
        } else {
            //what do we do here... the next major row, but they are not the same?
            //this is a problem as we don't support non-aligned columns with this code
            NSLog(@"CCVS error, something is wrong with the column configuration");
        }
    }
    //we reached the end assuming the same length
    //but first, what if the existing column has a few more
    //sections at the bottom
    if (i < [existingSections count]) {
        for (; i < [existingSections count]; i++) {
            [newColumn addSection:@"-" withTopics:nil withDetails:nil];
        }
    }
    //second, what if the newColumn has more sections than
    //the existing items...
    while ([newSections count]) {
        [newColumn addSection:newSections[0] withTopics:[column topicDataInSection:0] withDetails:[column detailDataInSection:0]];
        //for all existing sections we need to add an empty section...
        for (CCVSColumn *c in self.ccvsColumns) {
            [c addSectionAtIndex:i withTitle:@"-" withTopics:nil withDetails:nil];
        }
        [newSections removeObjectAtIndex:j];
        [column removeSetion:0];
    }
    [ccvsColumns addObject:newColumn];
    self.ccvsColumns = ccvsColumns;
}

#pragma mark - helpers to load data for the view

-(void)loadViewDataByNtcID:(NSString *)nctID fromSearch:(Search *)search
{
    CCVSColumn * studyView = [self loadColumnByNtcID:nctID fromSearch:search];
    
    //set dest vc data
    //self.ccvsColumns = @[studyView];
    [self addColumn:studyView];
    
    if ([self.ccvsColumns count] > 1) {
        self.titleBar.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoPressed:)];
    }
    
    if ([self.ccvsColumns count] > 2) {
        self.disableScaleToFit = YES;
    }
    
    //notify the collection that we have changes and need to reload.
    [self.collectionView reloadData];
}
- (IBAction)undoPressed:(UIBarButtonItem *)sender {
    NSMutableArray * reduced = [self.ccvsColumns mutableCopy];
    [reduced removeLastObject];
    if ([reduced count] < 2) {
        self.titleBar.rightBarButtonItem = nil;
    }
    if ([reduced count] < 3) {
        self.disableScaleToFit = NO;
    }    
    self.ccvsColumns = reduced;

    //notify the collection that we have changes and need to reload.
    [self.collectionView reloadData];
}

-(CCVSColumn*)loadColumnByNtcID:(NSString *)nctID fromSearch:(Search *)search
{
    //TODO: at some point revist where this helper method should really be located
    CCVSColumn * studyView = [[CCVSColumn alloc] init];
    
    ClinicalTrialGov *clinicalTrialGov = [[CommonAPI class]
                                          clinicalTrialGov:[NSString stringWithFormat:@"%@",nctID]
                                          fromSearch:search];
    if (clinicalTrialGov == nil || !clinicalTrialGov) {
        search = [[ClinicalTrialGov class] SearchForClinicalTrialGov:nctID
                                                                  context:[[DatabaseHelper class] database].managedObjectContext];
        clinicalTrialGov = [[CommonAPI class]
                            clinicalTrialGov:[NSString stringWithFormat:@"%@", nctID]
                            fromSearch:search];
    }
    
    //add first
    NSMutableArray * topics1 = [[NSMutableArray alloc] init];
    NSMutableArray * details1 = [[NSMutableArray alloc] init];
    [topics1 addObject:@"NCT Study ID"];
    [details1 addObject:nctID];
    if (clinicalTrialGov.briefTitle) {
        [topics1 addObject:@"Brief Title"];
        [details1 addObject:clinicalTrialGov.briefTitle];
    }
    if (clinicalTrialGov.phase) {
        [topics1 addObject:@"Phase"];
        [details1 addObject:clinicalTrialGov.phase];
    }
    if (clinicalTrialGov.study_type) {
        [topics1 addObject:@"Study Type"];
        [details1 addObject:clinicalTrialGov.study_type];
    }
    if (clinicalTrialGov.start_date) {
        [topics1 addObject:@"Start Date"];
        [details1 addObject:clinicalTrialGov.start_date];
    }
    if (clinicalTrialGov.status) {
        [topics1 addObject:@"Status"];
        [details1 addObject:clinicalTrialGov.status];
    }
    //
    NSMutableArray * conds = [[NSMutableArray alloc] init];
    for (Condition * cond in clinicalTrialGov.condition) {
        [conds addObject:cond.condition];
    }
    if ([conds count]) {
        CCVSListOfStrings * losCond = [[CCVSListOfStrings alloc] init];
        losCond.strings = conds;
        losCond.bgColor = [UIColor lightGrayColor];
        losCond.textColor = [UIColor blackColor];
        [topics1 addObject:@"Conditions"];
        [details1 addObject:losCond];
    }
    //
    [studyView addSection:@"Clinical Study" withTopics:topics1 withDetails:details1];
    //
    NSMutableArray * topics2 = [[NSMutableArray alloc] init];
    NSMutableArray * details2 = [[NSMutableArray alloc] init];
    //
    if  (clinicalTrialGov.city) {
        [topics2 addObject:@"City"];
        [details2 addObject:clinicalTrialGov.city];
    }
    //
    NSMutableArray * locs = [[NSMutableArray alloc] init];
    for (Location * loc in clinicalTrialGov.location) {
        [locs addObject:loc.city];
    }
    if ([locs count]) {
        CCVSListOfStrings * los = [[CCVSListOfStrings alloc] init];
        los.strings = locs;
        los.bgColor = [UIColor lightGrayColor];
        los.textColor = [UIColor blackColor];
        [topics2 addObject:@"Additional Locations"];
        [details2 addObject:los];
    }
    //
    [studyView addSection:@"Locations" withTopics:topics2 withDetails:details2];
    //
    NSMutableArray * topics3 = [[NSMutableArray alloc] init];
    NSMutableArray * details3 = [[NSMutableArray alloc] init];
    //
    if  (clinicalTrialGov.detailed_desc) {
        [topics3 addObject:@"Detailed Description"];
        CCVSString * cstr = [[CCVSString alloc] init];
        cstr.string = clinicalTrialGov.detailed_desc;
        cstr.segueInsteadOfExpand = YES;
        [details3 addObject:cstr];
    }
    //
    if (clinicalTrialGov.study_design ) {
        [topics3 addObject:@"Study Design"];
        [details3 addObject:clinicalTrialGov.study_design];
    }
    //
    if ([clinicalTrialGov.armGroups count]) {
        for (ArmGroups * ag in clinicalTrialGov.armGroups) {
            NSMutableArray *subt3 = [[NSMutableArray alloc] init];
            NSMutableArray *subd3 = [[NSMutableArray alloc] init];
            if (ag.desc) {
                [subt3 addObject:@"Description"];
                [subd3 addObject:ag.desc];
            }
            if (ag.arm_group_type) {
                [subt3 addObject:@"Arm Group Type"];
                [subd3 addObject:ag.arm_group_type];
            }
            NSString * t = (ag.arm_group_label != nil) ? ag.arm_group_label : @"";
            [topics3 addObject:t];
            if ([subt3 count]) {
                [details3 addObject:@[subt3,subd3]];
            } else {
                [details3 addObject:@""];
            }
        }
    }
    //
    [studyView addSection:@"Details" withTopics:topics3 withDetails:details3];
    //
    NSMutableArray * topics4 = [[NSMutableArray alloc] init];
    NSMutableArray * details4 = [[NSMutableArray alloc] init];
    //
    if ([clinicalTrialGov.eligibility count]) {
        int i = 1;
        for (Eligibility* e in clinicalTrialGov.eligibility) {
            if (e.criteria) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Criteria"]];
                CCVSString * cstr = [[CCVSString alloc] init];
                cstr.string = e.criteria;
                cstr.segueInsteadOfExpand = YES;
                [details4 addObject:cstr];
            }
            if (e.gender) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Gender"]];
                [details4 addObject:e.gender];
            }
            if (e.healthy_volunteers) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Health Volunteers"]];
                [details4 addObject:e.healthy_volunteers];
            }
            if (e.maximum_age) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Maximum Age"]];
                [details4 addObject:e.maximum_age];
            }
            if (e.minimum_age) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Minimum Age"]];
                [details4 addObject:e.minimum_age];
            }
            if (e.sampling_method) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Sampling Method"]];
                [details4 addObject:e.sampling_method];
            }
            if (e.study_pop) {
                [topics4 addObject:[NSString stringWithFormat:@"%d-%@",i,@"Study Population"]];
                [details4 addObject:e.study_pop];
            }
            i++; //advance i...
        }
    }
    [studyView addSection:@"Eligibility" withTopics:topics4 withDetails:details4];
    //
    NSMutableArray * topics5 = [[NSMutableArray alloc] init];
    NSMutableArray * details5 = [[NSMutableArray alloc] init];
    //
    if (clinicalTrialGov.intervention) {
        int i = 1;
        for (Intervention* interv in clinicalTrialGov.intervention) {
           NSMutableArray * subd5 = [[NSMutableArray alloc] init];
           NSString * t = (interv.name) ? interv.name : @"";
            if (interv.type) {
                [subd5 addObject:interv.type];
            }
            if (interv.desc) {
                [subd5 addObject:interv.desc];
            }
            [topics5 addObject:[NSString stringWithFormat:@"%d-%@",i,t]];
            [details5 addObject:@[subd5,[NSNull null]]];
            
            if (interv.armGroupLabel) {
                for (ArmGroupLabel * agl in interv.armGroupLabel) {
                    subd5 = [[NSMutableArray alloc] init];
                    [subd5 addObject:agl.name];
                }
                [topics5 addObject:[NSString stringWithFormat:@"%d-%@ - ARM Group Labels",i,t]];
                [details5 addObject:@[subd5,[NSNull null]]];
            }
            if (interv.otherName) {
                for (OtherName * ot in interv.otherName) {
                    subd5 = [[NSMutableArray alloc] init];
                    [subd5 addObject:ot.name];
                }
                [topics5 addObject:[NSString stringWithFormat:@"%d-%@ - Other Names",i,t]];
                [details5 addObject:@[subd5,[NSNull null]]];
            }
            
            i++; //next intervention
        }
    }
    //
    [studyView addSection:@"Intervention" withTopics:topics5 withDetails:details5];
    
    
    
    //add second
    if ([CommonAPI doesStudyContainAnalytics:clinicalTrialGov]) {
        for (NSDictionary * dictionary in [CCVSGraphData loadAllClinicalTrialGovGraphData:clinicalTrialGov]) {
            CCVSGraphData * plot = [[CCVSGraphData alloc] init];
            plot.chartType = [dictionary valueForKey:CHART_TYPE];
            plot.chartArray = [dictionary valueForKey:DATA];
            plot.chartTitle = [NSString stringWithFormat:@"%@",[dictionary valueForKey:DESCRIPTION]];
            //
            [studyView addSection:@"-" withTopics:@[plot.chartTitle] withDetails:@[plot]];
            //alternately consider
            //[studyView addSection:plot.chartTitle withTopics:@[plot] withDetails:nil];
        }
    }
    
    //add third
    [studyView addSection:@"Extras"
               withTopics:@[@"More Information"]
              withDetails:@[@[//topic detail sub combo
     //topic
     @[@"Web Resource 1",@"Web Resource 2"],
     //detail
     @[[[CCVSHyperLink alloc] initWithString:@"Clinical Collections"
                              andDestination:[CCVSHyperLink createClinicalCollectionsURLWithNTCID:nctID]],
     [[CCVSHyperLink alloc] initWithString:@"Clinical Trials.Gov"
                            andDestination:[CCVSHyperLink createClinicalTrialsGovURLWithNTCID:nctID]]
     ]
     ]]
     ];
    
    return studyView;
}

@end
